<?php

namespace api\modules\v1\controllers;
use api\common\controllers\UserController as U;
use api\modules\v1\models\Balancewater;
use Yii;
use yii\web\ServerErrorHttpException;
use yii\web\HttpException;
use api\common\models\Mar;
use \yii\caching\FileDependency;
use yii\web\NotFoundHttpException;
class UserController extends U
{
    public $modelClass = 'api\modules\v1\models\User';


    public function behaviors() {
        if(in_array(Yii::$app->controller->action->id,['list'])){ //排除评论列表权限验证
            $this->_is_auth = false;
        }
        return parent::behaviors();
    }

    public function actions()
    {
        $actions = parent::actions();
        unset($actions['index'],$actions['update'], $actions['create'], $actions['delete'], $actions['view']); //卸载操作
        return $actions;
    }

    /*
     * 获取用户列表
     * @param $catid
     */
    public function actionList($id){
        $model = new $this->modelClass();
        return $model->getList($id);
    }

    /*
     * 更换头像
     * @param $id
     * @return mixed
     */
    public function actionHeadimg(){
        $model = $this->findModel(Yii::$app->user->identity->id);
        $model->scenario='headimg';
        $model->attributes = Yii::$app->request->post();
        if ($model->save()) {
            return [
                "message"=>"更换头像成功!",
            ];
        }elseif (!$model->hasErrors()) {
            throw new ServerErrorHttpException('由于不明原因未能创建对象.');
        }else{
            throw new HttpException(401,array_values($model->getFirstErrors())[0],0);
        }
    }

    /*
     * 更换昵称
     * @param $id
     * @return mixed
     */
    public function actionNickname(){
        $model = $this->findModel(Yii::$app->user->identity->id);
        $model->scenario='nickname';
        $model->attributes = Yii::$app->request->post();


        //敏感词过滤
        $cahce = Yii::$app->cache;
        $cacheKey = 'comment_badword';
        $badword = $cahce->get($cacheKey);
        if ($badword === false) {
            $badword_model = new Mar('{{%badword}}');
            $badword = $badword_model->find()->asArray()->all();
            foreach($badword as $key=>$val){
               // $badword[$key]['badword'] = trim($val['badword'],PHP_EOL);
                $badword[$key]['badword'] = trim(str_replace(PHP_EOL, '', $val['badword']));
            }
            $cahce->set($cacheKey, $badword,Yii::$app->params['cache_time_comment_badword'],new FileDependency(['fileName' => 'cache.txt']));
        }

        foreach($badword as $value){
            $value['badword'] = trim($value['badword']);
            if(strpos($model->nickname,$value['badword']) !== false){
                throw new HttpException(401,'你的昵称带有敏感信息,操作失败!',0);
            }
        }
        if ($model->save()) {
            return [
                "message"=>"更换昵称成功!",
            ];
        }elseif (!$model->hasErrors()) {
            throw new ServerErrorHttpException('由于不明原因未能创建对象.');
        }else{
            throw new HttpException(401,array_values($model->getFirstErrors())[0],0);
        }
    }

    /*
     * 更换手机
     * @param $id
     * @return mixed
     */
    public function actionPhone(){
        $model = $this->findModel(Yii::$app->user->identity->id);
        if($model->phone != Yii::$app->request->post('phone')){
            throw new HttpException(401,'phone非法提交',0);
        }
        $model->scenario='phone';
        $model->attributes = Yii::$app->request->post();
        if ($model->phone()) {
            return [
                "message"=>"手机号码更改成功!",
            ];
        }elseif (!$model->hasErrors()) {
            throw new ServerErrorHttpException('由于不明原因未能创建对象.');
        }else{
            throw new HttpException(401,array_values($model->getFirstErrors())[0],0);
        }
    }

    /*
     * 更换密码
     * @param $id
     * @return mixed
     */
    public function actionPwd(){
        $model = $this->findModel(Yii::$app->user->identity->id);
        if($model->password_hash == ''){
            throw new HttpException(401,'您还未设置密码无法修改密码',0);
        }

        $model->scenario='pwd';
        $model->attributes = Yii::$app->request->post();
        $model->password_hash = Yii::$app->security->generatePasswordHash($model->password);//利用password_hash模式加密密码字符
        $model->access_token = $model->makeAccessToken($model->username,$model->password);
        if ($model->save()) {
            return [
                "message"=>"密码更改成功,请重新登录!",
            ];
        }elseif (!$model->hasErrors()) {
            throw new ServerErrorHttpException('由于不明原因未能创建对象.');
        }else{
            throw new HttpException(401,array_values($model->getFirstErrors())[0],0);
        }
    }

    /*
     * 获取用户信息
     * @param $id
     */
    public function actionInfo(){
        $model = new $this->modelClass();
        return $model->getInfo();
    }

    /*
     * 绑定提现账户
     * @param $id
     * @return mixed
     */
    public function actionWithdraw(){
        $model = $this->findModel(Yii::$app->user->identity->id);
        $model->scenario='withdraw';
        $post = Yii::$app->request->post();
        if(($post['type'] == 1 && $post['withdraw_zfb'] == '') || ($post['type'] == 2 && $post['withdraw_wx'] == '')){
            throw new HttpException(401,'账户不能为空',0);
        }
        if(($post['type'] == 1 && $model->withdraw_wx == '') || ($post['type'] == 2 && $model->withdraw_zfb == '')){
            $model->withdraw_default = $post['type'];
        }
        $model->attributes = $post;
        if ($model->save()) {
            return [
                "message"=>"账户绑定成功!",
            ];
        }elseif (!$model->hasErrors()) {
            throw new ServerErrorHttpException('由于不明原因未能创建对象.');
        }else{
            throw new HttpException(401,array_values($model->getFirstErrors())[0],0);
        }
    }
    /*
     * 删除提现账户
     * @param $id
     * @return mixed
     */
    public function actionWithdrawdel(){
        $model = $this->findModel(Yii::$app->user->identity->id);
        $post = Yii::$app->request->post();
        if(!isset($post['type']) || $post['type'] == ''){
            throw new HttpException(401,'参数错误',0);
        }
        $model->attributes = $post;
        if($post['type'] == 1){
            $model->withdraw_zfb = '';
            if($model->withdraw_default == 1  && $model->withdraw_wx != ''){
                $model->withdraw_default = 2;
            }elseif($model->withdraw_wx == ''){
                $model->withdraw_default = 0;
            }
        }elseif($post['type'] == 2){
            $model->withdraw_wx = '';
            if($model->withdraw_default == 2  && $model->withdraw_zfb != ''){
                $model->withdraw_default = 1;
            }elseif($model->withdraw_zfb == ''){
                $model->withdraw_default = 0;
            }
        }
        if ($model->save()) {
            return [
                "message"=>"删除成功!",
            ];
        }elseif (!$model->hasErrors()) {
            throw new ServerErrorHttpException('由于不明原因未能创建对象.');
        }else{
            throw new HttpException(401,array_values($model->getFirstErrors())[0],0);
        }
    }
    /*
     * 提现密码设置、修改、找回
     * @param $id
     * @return mixed
     */
    public function actionWithdrawpass(){
        $model = $this->findModel(Yii::$app->user->identity->id);
        $model->scenario='withdrawpass';
        $model->attributes = Yii::$app->request->post();
        if(strlen($model->withdraw_password) != 6){
            throw new HttpException(401,'提现密码必须为6位',0);
        }
        $model->withdraw_password = Yii::$app->security->generatePasswordHash($model->withdraw_password);//利用password_hash模式加密提现密码字符
        if ($model->save()) {
            return [
                "message" => "提现密码设置成功!",
            ];
        }elseif (!$model->hasErrors()) {
            throw new ServerErrorHttpException('由于不明原因未能创建对象.');
        }else{
            throw new HttpException(401,array_values($model->getFirstErrors())[0],0);
        }
    }

    /*
     * 提现密码验证
     * @param $id
     * @return mixed
     */
    public function actionWithdrawpassyz(){
        $model = $this->findModel(Yii::$app->user->identity->id);
        $post = Yii::$app->request->post();
        if(!isset($post['withdraw_password']) || strlen($post['withdraw_password']) != 6){
            throw new HttpException(401,'提现密码必须为6位',0);
        }
        if($model->withdrawpass_num >= 3 && (time() - $model->withdraw_lock_time) < 24*3600){//24*3600
            throw new HttpException(401,'您的提现账户已被锁定，一天后将自动解冻',0);
        }
        if($model->withdrawpass_num >= 3 && (time() - $model->withdrawpass_num) >= 24*3600){//24*3600
            $model->withdraw_lock_time = 0;
            $model->withdrawpass_num = 0;
        }
        if(!Yii::$app->security->validatePassword($post['withdraw_password'], $model->withdraw_password)){
            $model->withdrawpass_num += 1;
            if($model->withdrawpass_num == 2){
                $model->save();
                throw new HttpException(401,'您已连续输入错误2次，再次输入错误该账户将被冻结，您可以尝试找回提现密码',0);
            }elseif($model->withdrawpass_num == 3){
                $model->withdraw_lock_time = time();
                $model->save();
                throw new HttpException(401,'您的提现账户已被锁定，一天后将自动解冻',0);
            }
            $model->save();
            throw new HttpException(401,'提现密码错误',0);

        }else{
            $model->withdrawpass_num = 0;
            $model->withdraw_lock_time = 0;
            $model->save();
            return [
                "message" => "提现密码验证成功!",
            ];
        }
    }

    /*
     * 提现密码状态
     * @param $id
     * @return mixed
     */
    public function actionWithdrawpasszt(){
        $model = $this->findModel(Yii::$app->user->identity->id);
        if($model->withdraw_password == ''){
            return [
                "message" => '0',//未设置提现密码
            ];
        }elseif($model->withdrawpass_num >= 3 && (time() - $model->withdraw_lock_time) < 24*3600){//24*3600
            return [
                "message" => '1',//提现账户已锁定
            ];
        }else{
            return [
                "message" => '2',//密码状态正常
            ];
        }
    }
    /*
     * 设置提现默认账户
     * @param $id
     * @return mixed
     */
    public function actionWithdrawdefault(){
        $model = $this->findModel(Yii::$app->user->identity->id);
        $model->scenario='withdrawdefault';
        $post = Yii::$app->request->post();
        if(!isset($post['withdraw_default']) || ($post['withdraw_default'] != 1 && $post['withdraw_default'] != 2)){
            throw new HttpException(401,'无效的参数',0);
        }
        $model->attributes = $post;
        if ($model->save()) {
            return [
                "message" => "设置默认账户成功!",
            ];
        }elseif (!$model->hasErrors()) {
            throw new ServerErrorHttpException('由于不明原因未能创建对象.');
        }else{
            throw new HttpException(401,array_values($model->getFirstErrors())[0],0);
        }
    }
    /*
     * 提现协议
     * @param $id
     * @return mixed
     */
    public function actionWithdrxieyi(){
        $model = $this->findModel(Yii::$app->user->identity->id);
        $post = Yii::$app->request->post();
        if(!isset($post['withdraw_xieyi']) || ($post['withdraw_xieyi'] != 1)){
            throw new HttpException(401,'无效的参数',0);
        }
        $model->withdraw_xieyi = $post['withdraw_xieyi'];
        if ($model->save()) {
            return [
                "message" => "设置成功!",
            ];
        }elseif (!$model->hasErrors()) {
            throw new ServerErrorHttpException('由于不明原因未能创建对象.');
        }else{
            throw new HttpException(401,array_values($model->getFirstErrors())[0],0);
        }
    }
    /*
     * 提现规则
     * @param $id
     * @return mixed
     */
    public function actionWithdrrule(){
        $model = $this->findModel(Yii::$app->user->identity->id);
        $post = Yii::$app->request->post();
        if(!isset($post['withdraw_rule']) || ($post['withdraw_rule'] != 1)){
            throw new HttpException(401,'无效的参数',0);
        }
        $model->withdraw_rule = $post['withdraw_rule'];
        if ($model->save()) {
            return [
                "message" => "设置成功!",
            ];
        }elseif (!$model->hasErrors()) {
            throw new ServerErrorHttpException('由于不明原因未能创建对象.');
        }else{
            throw new HttpException(401,array_values($model->getFirstErrors())[0],0);
        }
    }

    /*
     * 收益总览
     */
    public function actionBalancewater(){
        $model = new Balancewater();
        return $model->getBalancewater();
    }

}
